// pages/car_order/appraise/appraise.js
var api = require('../../../api/api');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectIndex: 0,
    type: 0, // 类型:0=评价洗车,1=评价商品  
    content: '',
    imgArr: [],
    content: '',
    order_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: this.data.type,
      detail_id: options.detail_id
    })

  },
  selectedStar(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectIndex: index
    })
  },
  apprise() {
    if (!this.data.content) {
      wx.showToast({
        title: '请填写评语',
        icon: 'none'
      })
      return false;
    }

    api.post('/user/apprise', {
      type: this.data.type,
      detail_id: this.data.detail_id,
      score: this.data.selectIndex + 1,
      content: this.data.content,
      images: this.data.imgArr
    }).then(res => {
      wx.showToast({
        title: '提交成功',
      })
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        'info.status':3
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)
    })


  },

  //从本地获取照片 
  getLocalityImg() {
    let that = this;
    if (this.data.imgArr.length < 5) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'], //可以指定原图或压缩图,默认二者都有
        sourceType: ['album', 'camera'], //指定图片来源是相机还是相册,默认二者都有
        success(res) {
          console.log(res, "---------上传的图片")

          const tempFiles = res.tempFiles[0].path //包含图片大小的数组
          wx.showLoading({
            title: '上传图片中',
            mask: true
          })
          wx.uploadFile({
            header: {
              'Content-Type': 'application/json; charset=UTF-8',
              'X-Requested-With': 'XMLHttpRequest',
              'Authorization': wx.getStorageSync('token')
            },
            filePath: tempFiles,
            name: 'file',
            url: api.httpUrl + "/common/upload",
            success: res => {
              wx.hideLoading({
                success: (res) => {},
              })
              let upload_data = res.data;
              upload_data = JSON.parse(upload_data)

              const upload_image = upload_data.data.url;
              console.log("upload_image", upload_image)
              let imgArr = that.data.imgArr;
              imgArr.push(upload_image)
              that.setData({
                imgArr
              })
            }
          })


        }
      })

    } else {
      wx.showToast({ //超过图片张数限制提示
        title: '最多上传五张图片',
        icon: 'none',
        duration: 2000
      })

    }
  },

  //删除照片功能与预览照片功能 
  deleteImg(e) {
    let that = this;
    let imgArr = that.data.imgArr;
    let index = e.currentTarget.dataset.index; //获取长按删除图片的index
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success(res) {
        if (res.confirm) {
          // console.log('点击确定了');
          imgArr.splice(index, 1);
        } else if (res.cancel) {
          // console.log('点击取消了');
          return false;
        }
        that.setData({
          imgArr: imgArr
        });
      }
    })
  },

  //预览图片
  previewImg(e) {
    let index = e.currentTarget.dataset.index;
    let imgArr = this.data.imgArr;
    wx.previewImage({
      current: imgArr[index],
      urls: imgArr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})